package gruppe7.servlets;

import gruppe7.control.ServletController;
import gruppe7.model.Model;
import gruppe7.model.Station;
import gruppe7.util.Html;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class EditStationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Html html = new Html();
		Model model = Model.getInstance();
		PrintWriter out = response.getWriter();
		RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");
		ServletController sc = ServletController.getInstance();

		rd.include(request, response);

		out.println(html.setHeader("Edit station"));
		out.println("<div id ='content'>");
		out.println("<div id='content-left'>");
		out.println("<h3><img src='images/edit.png' /> Edit Station</h3><br/>");

		String stationName = request.getParameter("station");
		String oldName = stationName;
		
		Station station = null;
		
		try {
			station = model.getStationByName(oldName);
		} catch (Exception e) {
			out.println("Database-fejl: " + e);
		}
		
		int zone = station.getZone();
		
		out.println("<p>"
				+ "<table class='default'>"
				+ "<form action='edit_stationinfo.do' method='post'>"
				+ "<tr><td>Edit name </td><td><input type='text' name='editedName' value='" + stationName + "'/></td></tr>"
				+ "<tr><td>Edit zone </td><td><input type='text' name ='editedZone' value='" + zone + "'/>"
				+ "<input type='hidden' name ='oldName' value='" + oldName + "'/></td></tr>"
				+ "<tr><td>Edit comment </td><td><input type='text' name='comment' value='" + station.getComment() + "'/></td></tr>"
				+ "<tr><td>Edit manager </td><td><input type='text' name='manager' value='" + station.getManager() + "'/></td></tr>"
				+ "<tr><td>Edit erection date </td><td><input type='text' name='erectiondate' value='" + station.getErectionDate() + "'/></td></tr>"
				+ "<tr><td></td><td align='right'><input class='submit' type='submit' value='Save'/></td></tr>"
				+ "</form>"
				+ "</table>"
				+ "</p>"
				+ "<br /><p><a href='javascript: history.go(-1)'>Back</a></P>");

		out.println("</div></div>");
		out.println(html.setFooter());

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


}
